﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Common.EntityValidation
{
    public class RegExValidationAttribute : ValidationAttribute
    {
          


        public bool NullAble { get; set; }  //可否为空
        public string RegEx { get; set; }

        public static ValidResultDetail IsValid(object input)
        {
            var r = new ValidResultDetail();

            var rgv = new RegExValidationAttribute();
            r.IsValid= rgv.Verify(input);
            r.ErrorMessage = rgv.ErrorMessage;
            return r;
        }


        public override bool Verify(object o)
        {

            string input = null;
            if (o != null)
            {
                input = o.ToString();
            }

            if (string.IsNullOrEmpty(input) && (!NullAble))

            {

                return false;

            }
            var r = false;



            if (string.IsNullOrWhiteSpace(RegEx))
            {
                return r;
            }


            return Regex.IsMatch(input, RegEx);
        }
    }
}